package mysql

import (
	"gorm.io/gorm"
	"vip-project/global"
)

//用户表

type User struct {
	gorm.Model
	Username string  `gorm:"type:varchar(20);not null;"`
	Password string  `gorm:"type:char(32);not null;"`
	Level    string  `gorm:"type:varchar(20);not null;"`
	Name     string  `gorm:"type:varchar(20);not null;"`
	Sex      string  `gorm:"type:varchar(3);not null;"`
	Email    string  `gorm:"type:varchar(50);not null;"`
	Money    float64 `gorm:"type:decimal(10,2);not null;"`
	Score    int64   `gorm:"type:int(10);not null;"`
	Phone    string  `gorm:"type:char(11);not null;"`
	Status   int8    `gorm:"type:tinyint(1);default:1;comment:1正常,2异常"`
}

//会员注册

func (u *User) Register() error {
	return global.Db.Create(&u).Error
}

//会员列表

func (u *User) List() ([]*User, error) {
	var list []*User
	err = global.Db.Where("username like ?", "%"+u.Username+"%").Find(&u).Error
	return list, err
}

//登录

func (u *User) Login() error {
	return global.Db.Where("username = ?", u.Username).Find(&u).Limit(1).Error
}

//修改

func (u *User) Update(username string) error {
	return global.Db.Model(&u).Update("username = ?", username).Error

}

//删除

func (u *User) Delete(id int) error {
	return global.Db.Model(&u).Delete("id = ?", id).Error
}
